#define  _CRT_SECURE_NO_WARNINGS
#pragma once

#include "SeqList.h"

void InitSeqList(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void DestroySeqList(SL* ps)
{
	free(ps->arr);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void PrintSeqList(SL* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int NewCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SQDataType* tmp = (SQDataType*)realloc(ps->arr, sizeof(SQDataType) * NewCapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
		}
		else
		{
			ps->arr = tmp;
			ps->capacity = NewCapacity;
		}
		
	}
}

void PushBack(SL* ps, SQDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;

}

void PushFront(SL* ps, SQDataType x)
{
	assert(ps);
	SLCheckCapacity(&ps);
	int i = 0;
	for (i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

void PopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);

	ps->size--;
}

void PopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	int i = 0;
	for (i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

void SeqListInsert(SL* ps, int pos, SQDataType x)
{
	assert(ps);
	assert(pos <= ps->size);
	int i = 0;
	for (i = ps->size; i >= pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[i] = x;
	ps->size++;
}

void SeqListErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos <= ps->size);
	int i = 0;
	for (i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//SQDataType SeqListFind(SL* ps, SQDataType x)
//{
//	assert(ps);
//	int i = 0;
//	for (i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//		{
//			return i;
//		}
//	}
//	return -1;
//}

void SeqListModity(SL* ps, int pos, SQDataType x)
{
	assert(ps);
	assert(pos <= ps->size);
	ps->arr[pos] = x;
}